package com.zdkj.subway.st.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zdkj.common.security.service.PigxUser;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.st.constant.enums.StockEnum;
import com.zdkj.subway.st.entity.SwStOutputDetail;
import com.zdkj.subway.st.entity.SwStTicketReceive;
import com.zdkj.subway.st.mapper.SwStTicketReceiveMapper;
import com.zdkj.subway.st.service.SwStTicketReceiveService;
import com.zdkj.subway.st.util.GenSerialNo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * @ProjectName: zdkj
 * @Package: com.zdkj.subway.st.service.impl
 * @ClassName: SwStTicketReceiveServiceImpl
 * @Description:
 * @Author: kangjian
 * @CreateDate: 2020/1/8 14:48
 * @Version: 1.0
 */
@Slf4j
@Service
@AllArgsConstructor
public class SwStTicketReceiveServiceImpl extends ServiceImpl<SwStTicketReceiveMapper, SwStTicketReceive> implements SwStTicketReceiveService {
    @Autowired
    private SwStOutputDetailServiceImpl sdcd;

    @Autowired
    private GenSerialNo genSerialNo;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(SwStTicketReceive swStTicketReceive) {
        boolean issuccuess=true;
        String stocktype= StockEnum.StockType.OUTSTOCK.getValue();
        //销毁出库
        String outstockvalue=StockEnum.OutputStock.LYCK.getValue();
        try {
            //根据票种生成单号前缀
            String prefix = genSerialNo.getPreFixByTicketType(swStTicketReceive.getReceiveCardType());
            String serialno=genSerialNo.genSerioNo(stocktype);
            PigxUser user = SecurityUtils.getUser();
            //执行出库操作
            SwStOutputDetail swStOutputDetail=SwStOutputDetail.builder().creatorId(user.getId()).creatorName(user.getUsername())
                    .ticketType(swStTicketReceive.getReceiveCardType()).outGoingType(outstockvalue).createTime(LocalDateTime.now()).outstockno(prefix+serialno)
                    .stockArea(swStTicketReceive.getStockArea()).ticketNumber(swStTicketReceive.getReceiveNumber()).build();
            sdcd.save(swStOutputDetail);
            swStTicketReceive.setCreatorName(user.getUsername());
            swStTicketReceive.setCreatorId(user.getId());
            //swStTicketReceive
            swStTicketReceive.setOutputStockid(swStOutputDetail.getId());
            swStTicketReceive.setOutputStockno(prefix+serialno);
        }catch (Exception e){
            log.error("",e);
            throw new RuntimeException(e);
        }
        return issuccuess&&super.save(swStTicketReceive);
    }

    /**
     * 通过库存更新领用信息
     * @param swStOutputDetail
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean save(SwStOutputDetail swStOutputDetail) {
        try {
            SwStTicketReceive swStTicketReceive=new SwStTicketReceive();
            swStTicketReceive.setOutputStockno(swStOutputDetail.getOutstockno());
            swStTicketReceive.setReceiveCardType(swStOutputDetail.getTicketType());
            swStTicketReceive.setActiveTime(swStOutputDetail.getActiveTime());
            swStTicketReceive.setTicketNo(swStOutputDetail.getTicketNo());
            swStTicketReceive.setReceiveNumber(swStOutputDetail.getTicketNumber());
            swStTicketReceive.setStockArea(swStOutputDetail.getStockArea());
            swStTicketReceive.setOutStorageTime(swStOutputDetail.getOutGoingTime());
            swStTicketReceive.setIsReceive("否");
            swStTicketReceive.setTicketName(swStOutputDetail.getTicketName());
            swStTicketReceive.setTicketPrice(swStOutputDetail.getTicketPrice());
            swStTicketReceive.setTicketTimes(swStOutputDetail.getTicketTimes());
            swStTicketReceive.setOutputStockid(swStOutputDetail.getId());
			swStTicketReceive.setOutputPersonCode(swStOutputDetail.getKeepercode());//出库人  
			swStTicketReceive.setOutputPersonName(swStOutputDetail.getKeepername());//出库人工号
            swStTicketReceive.setLatestStartTime(swStOutputDetail.getLatestStartTime());
            swStTicketReceive.setFile(swStOutputDetail.getFilePath());
            swStTicketReceive.setPicture(swStOutputDetail.getPicturePath());
            return super.save(swStTicketReceive);
        }catch (Exception e){
            log.error("",e);
            throw new RuntimeException(e);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(SwStTicketReceive swStTicketReceive){
        SwStOutputDetail swStOutputDetail=sdcd.getById(swStTicketReceive.getOutputStockid());
        swStOutputDetail.setIsreceive("是");
        //首先更新库存
        if(! sdcd.updateById(swStOutputDetail))
        {
            throw new RuntimeException("更新库存异常!");
        }
        swStTicketReceive.setIsReceive("是");
        //更新入库数据
        return super.updateById(swStTicketReceive);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeById(Serializable id) {
        //级联删除库存
        SwStTicketReceive SwStTicketReceive= super.getById(id);
        long outputStockid= SwStTicketReceive.getOutputStockid();
        if(! sdcd.removeById(outputStockid))
        {
            throw new RuntimeException("更新库存异常!");
        }
        return super.removeById(id);
    }
}
